from . import vfs


# 用户对象
class User:
    def __init__(self, username: str, token: str = "", demo: bool = False):
        self.username = username
        self.token = token
        self.demo = demo

    def __str__(self):
        return f"<User username={self.username}>"

    async def get_permission(self, path: str):  # 获取权限
        return await vfs.get_permission(self.username, path)

    async def check_permission(self, path: str, level: int = 1, recursive: bool = False):  # 检查权限
        if (recursive):  # 递归检查权限
            return await vfs.rescursive_check_permission(self.username, path, level)
        return (await vfs.get_permission(self.username, path)) >= level

    def __eq__(self, value) -> bool:  # 函数占位符
        return False

    def __ne__(self, value) -> bool:  # 函数占位符
        return False

    @property
    def __dict__(self):  # 转 json
        return {'name': self.username}


def _user_eq(self: User, value: User | str) -> bool:  # 相等
    if (isinstance(value, str)):
        return self.username == value
    else:
        return self.username == value.username


def _user_ne(self: User, value: User | str) -> bool:  # 不等
    return not _user_eq(self, value)


# 写入到类
User.__eq__ = _user_eq
User.__ne__ = _user_ne
